﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using RLZY.others;
using Infragistics.Win.UltraWinGrid;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
namespace RLZY
{
    public partial class FormKey_GDX_Modify : Form
    {
        public FormKey_GDX_Modify()
        {
            InitializeComponent();
            
        }
        //public FormBDX_Modify(string username,String 权限)
        //{
        //    InitializeComponent();
        //    this.userID= strTextBox1Text;
        //}
        private static FormKey_GDX_Modify childFromInstanc;
        static log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public String userID;
        public String 权限;


        public static FormKey_GDX_Modify ChildFromInstanc
        {
            get
            {
                if (childFromInstanc == null || childFromInstanc.IsDisposed)
                //判断childFromInstanc是否有值，不为null或者窗体释放（当你将子窗体关了的时候）为childFromInstanc赋值
                {
                    childFromInstanc = new FormKey_GDX_Modify();
                }
                return childFromInstanc;
                //返回的childFromInstanc（子窗体存在时返回的是null,关闭了（IsDisposed为true）或者第一次打开子窗体返回一个ChildForm实例）
            }
        }

        private void FormNotKey_GDX_Modify_Load(object sender, EventArgs e)
        {
            try
            {
                BLL.userpassword bllpwd = new BLL.userpassword();
                Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
                others.myGZ mygz = new myGZ();
                权限 = model.备注;
                //this.txtBM.Text = 权限;

                //DataSet ds = mygz.GetListPart(0, 权限);
                //this.BindData(ds);
                this.BindListInfo();
            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void BindListInfo()
        {
            others.myGZ mygz = new myGZ();
            others.NotKey notkey = new NotKey();
            cmbSearchBZ.Items.Clear();
            cmbBM.Items.Clear();

            try
            {
                DataSet dsgz = mygz.GetListBM(权限);
                cmbSearchBZ.DataSource = dsgz.Tables[0];
                cmbSearchBZ.DisplayMember = "车间班组";  //列名1 
                cmbSearchBZ.ValueMember = "车间班组";    //列名2 
                DataSet dsbm = notkey.GetListBM();
                cmbBM.DataSource = dsbm.Tables[0];
                cmbBM.DisplayMember = "部门单位";  //列名1 
                cmbBM.ValueMember = "部门单位";    //列名2 
                
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
            }
        }

        private void BindData(DataSet ds)
        {
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            //dv.Sort = "ID desc";//反排序
            //bindingSource1.DataSource = dv;
            this.ultraGrid1.DataSource = dv;
        }
        //列选择器
        private void btnCustomColumnChooser_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowCustomColumnChooserDialog();
            }
            catch (Exception ex) { LOG.Error(ex); }
        }
        private RLZY.CustomColumnChooser customColumnChooserDialog = null;
        
        private void ShowCustomColumnChooserDialog()
        {
            if (null == this.customColumnChooserDialog || this.customColumnChooserDialog.IsDisposed)
            {
                this.customColumnChooserDialog = new RLZY.CustomColumnChooser();
                this.customColumnChooserDialog.Owner = this;
                this.customColumnChooserDialog.Grid = this.ultraGrid1;
            }

            this.customColumnChooserDialog.Show();
        }
        //修改列布局
        private void btnDisplayLayout_Click(object sender, EventArgs e)
        {
            ultraGrid1.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
        }
        //隐藏列
        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            e.Layout.Bands[0].Columns[0].Hidden = true;
        }

        private void cmbBZ_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        int id;

        private void ultraGrid1_DoubleClickCell(object sender, DoubleClickCellEventArgs e)
        {
            try
            {
                int i = this.ultraGrid1.ActiveRow.Index;
                id = Convert.ToInt32(this.ultraGrid1.Rows[i].Cells[0].Value.ToString().Trim());
                others.UserHelper.GZID = this.ultraGrid1.Rows[i].Cells[0].Value.ToString().Trim();
                Form关键固定项修改 fr = new Form关键固定项修改();
                fr.ShowDialog();
                if (fr.flesh)
                {
                    BLL.userpassword bllpwd = new BLL.userpassword();
                    Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
                    others.NotKey notkey = new NotKey();
                    权限 = model.备注;

                    DataSet ds = notkey.searchKey(this.cmbBM.Text.Trim(), this.cmbSearchBZ.Text.Trim(), this.txtSearchName.Text.Trim());
                    this.BindData(ds);
                }
                
            }
            catch(Exception ex)
            {
                LOG.Error(ex);
            }

        }
        public void Last_Date()
        {
            others.myGZ mygz = new myGZ();
            DataSet dataSet = mygz.GetListLastDate(权限);
           
            bool flag = dataSet.Tables[0].Rows.Count > 0;
            if (flag)
            {
                this.LastDate = dataSet.Tables[0].Rows[0]["日期"].ToString();
            }
            else
            {
                this.LastDate = "9998-12-31";
            }
            
        }
        
        public string LastDate { get; set; }

        
      
        public string Dat { get; set; }

        
       
        
        //按回车键跳转

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter && !(ActiveControl is Button))
            {//碰到Button时不跳转到下一个控件，而是执行Button的单击事件
                keyData = Keys.Tab;
            }
            return base.ProcessDialogKey(keyData);
        }

       
        
        public string VarDat { get; set; }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BLL.userpassword bllpwd = new BLL.userpassword();
            Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
            others.NotKey notkey = new NotKey();
            权限 = model.备注;

            DataSet ds = notkey.searchKey(this.cmbBM.Text .Trim (), this.cmbSearchBZ.Text.Trim(),this.txtSearchName.Text .Trim ());
            this.BindData(ds);
        }

        
    }
}
